<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>修改任务</title>
    <link rel="stylesheet" th:href="@{/css/base.css}" media="all">
    <link rel="stylesheet" th:href="@{/layui/css/layui.css}" media="all">
    <script type="text/javascript" th:src="@{/layui/layui.js}"></script>
    <script type="text/javascript" th:src="@{/js/jquery-3.2.0.min.js}"></script>
    <script type="text/javascript" th:src="@{/js/base.js}"></script>
    <style>
        body {
            overflow-y: auto;
            margin: 15px 0 15px 0;
        }
    </style>
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">

        <form class="layui-form" style="margin: 0 auto; max-width: 800px" action="" lay-filter="formDemo">
            <div class="layui-form layuimini-form">
                <input type="hidden" name="id" th:value="${data.id}">
                <div class="layui-form-item">
                    <label class="layui-form-label required">cron</label>
                    <div class="layui-input-block">
                        <input type="text" name="cron" lay-verify="required" lay-reqtext="请输入cron表达式" placeholder="请输入" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label required">是否压缩</label>
                    <div class="layui-input-block" style="display: flex; align-items: center">
                        <input type="checkbox" name="isCompress" lay-text="是|否" lay-skin="switch" value="true">
                        <tip>将被压缩成zip格式的文件，提升备份速度</tip>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label required">是否上传</label>
                    <div class="layui-input-block" style="display: flex; align-items: center">
                        <input type="checkbox" name="isUpload" lay-text="是|否" lay-skin="switch" value="true" lay-filter="isUpload">
                        <tip>将上传至文件服务器</tip>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label required">是否删除</label>
                    <div class="layui-input-block" style="display: flex; align-items: center">
                        <input type="checkbox" name="isDelete" lay-text="是|否" lay-skin="switch" value="true" lay-filter="isDelete">
                        <tip>将删除磁盘上的备份文件，取决于是否开启上传</tip>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label required">是否启动</label>
                    <div class="layui-input-block">
                        <input type="checkbox" name="status" lay-text="是|否" lay-skin="switch" value="true">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">备注</label>
                    <div class="layui-input-block">
                        <textarea name="remark" placeholder="请输入" class="layui-input" style="resize: none; min-height: 100px"></textarea>
                    </div>
                </div>
                <div class="layui-form-item">
                    <div class="layui-input-block">
                        <button class="layui-btn layui-btn-sm layui-btn-normal" lay-submit lay-filter="saveBtn">
                            保存
                        </button>
                    </div>
                </div>
            </div>
        </form>
    </div>
</div>

<script type="text/javascript" th:inline="javascript">

    layui.use(['form', 'layer'], function () {
        const form = layui.form,
            layer = layui.layer;

        // 初始化赋值
        form.val("formDemo", [[${data}]]);
        form.val("formDemo", [[${data.param}]]);

        // 是否上传选择
        form.on('switch(isUpload)', function (data) {
            let value = form.val('formDemo').isUpload
            if (!value) {
                value = false
            }
            form.val('formDemo', {
                'isDelete': value
            })
            form.render(null, 'isDelete');
        })

        // 监听提交
        form.on('submit(saveBtn)', function (res) {
            const data = res.field;
            // 是否启动
            if(!data.status) {
                data.status = false
            }
            // 是否上传
            if(!data.isUpload) {
                data.isUpload = false
            }
            // 是否删除
            if(!data.isDelete) {
                data.isDelete = false
            }
            // 提交后台
            Asurplus.post({
                url: ctxPath + 'backup-task/update',
                data: JSON.stringify(data)
            }, () => {
                layer.msg('操作成功', {icon: 1});
                setTimeout(function () {
                    const windowIndex = parent.layer.getFrameIndex(window.name);
                    parent.layer.close(windowIndex);
                }, 1000)
            });
            return false;
        });
    });
</script>
</body>
</html>
